setScreenMetrics(device.width, device.height);
let current_package = "com.ss.android.ugc.aweme.lite";
function launchAppEnhance(app_name) {
    let result;
    let thread = threads.start(function () {
        toastLog("检查是否有打开权限!");
        while (true) {
            var allow = text("允许").findOne(5000);
            if (allow) {
                sleep(1000)
                toastLog("发现允许授权!");
                allow.click();
                sleep(1000);
                thread.interrupt();
                break;
            }
        }
    });
    if (currentPackage() != current_package) {
        result = app.launchApp(app_name);
        sleep(3000);
    }
    else {
        result = currentPackage();
    }
    thread.interrupt();
    return result;
}

function bezier_curves(cp, t) {
    cx = 3.0 * (cp[1].x - cp[0].x);
    bx = 3.0 * (cp[2].x - cp[1].x) - cx;
    ax = cp[3].x - cp[0].x - cx - bx;
    cy = 3.0 * (cp[1].y - cp[0].y);
    by = 3.0 * (cp[2].y - cp[1].y) - cy;
    ay = cp[3].y - cp[0].y - cy - by;

    tSquared = t * t;
    tCubed = tSquared * t;
    result = { "x": 0, "y": 0 };
    result.x = (ax * tCubed) + (bx * tSquared) + (cx * t) + cp[0].x;
    result.y = (ay * tCubed) + (by * tSquared) + (cy * t) + cp[0].y;
    return result;
};


function sml_move(qx, qy, zx, zy, time) {
    //仿真随机带曲线滑动: qx, qy, zx, zy, time 代表起点x,起点y,终点x,终点y,过程耗时单位毫秒
    var xxy = [time];
    var point = [
        { "x": qx, "y": qy },
        { "x": random(qx - 100, qx + 100), "y": random(qy, qy + 50) },
        { "x": random(zx - 100, zx + 100), "y": random(zy, zy + 50) },
        { "x": zx, "y": zy }
    ];
    for (let i = 0; i < 1; i += 0.08) {
        xxyy = [parseInt(bezier_curves(point, i).x), parseInt(bezier_curves(point, i).y)]
        xxy.push(xxyy);
    }
    gesture.apply(null, xxy);
}


app_func = {}
app_func.module_name = "抖音极速版";

function close_ad() {
    let $times = 15;
    var $close_button = text("关闭广告").findOne(1000)
    while (!$close_button) {
        if ($times <= 0) { break; }
        $close_button = text("关闭广告").findOne(1000)
        if ($close_button) { break; }
        $times--; sleep(4000);
    }
    toastLog("关闭广告");
    try { $close_button.click(); } catch (e) { }
}

function check_guankanshipin() {
    var widget = text("观看视频领更多金币").findOne(5000);
    if (widget) {
        toastLog("发现观看视频领更多金币")
        widget.click(); //点击 观看视频领更多金币
        toastLog("开始点击观看视频领更多金币")
        sleep(20000);
        close_ad();
    }
    //点击宝箱后有 看视频再赚xxx金币
    var video = text("看视频再赚").findOne(1000);
    if (video) {
        toastLog("发现看视频再赚,点击");
        click(video.bounds().centerX(), video.bounds().centerY());
        sleep(20000);
        close_ad();
    }
    //看广告视频再赚
    var widget = textContains("看广告视频再赚").findOne(1000);
    if (widget) {
        toastLog("发现看广告视频再赚")
        widget.click(); //点击 看广告视频再赚
        toastLog("开始看广告视频再赚")
        sleep(20000);
        close_ad();
    }
}

function check_kaibaoxiang() {
    let kaibaoxiang = id("com.ss.android.ugc.aweme.lite:id/az7").findOne(1000) || text('开宝箱').findOne(1000)
    if (kaibaoxiang) {
        log("发现开宝箱");
        setScreenMetrics(device.width, device.height);
        var shouye = text("首页").findOne(2000);
        var y = shouye.bounds().centerY();
        var x = device.width / 2;
        click(x, y);
        sleep(2000);
        var 限时任务赚金币 = text('限时任务赚金币').findOne(3000)
        if (限时任务赚金币) {
            var qulinqu = 限时任务赚金币.parent().parent().findOne(text('去领取'))
            if (qulinqu) {
                log("限时任务赚金币!点击去领取");
                sleep(1000)
                qulinqu.click();
                close_ad();
                sleep(1000);
            }
        }
        //领百元红包广告
        var 图片 = text("图片").depth("15").findOne(2000)
        try {
            图片.parent().child(1).click()
        } catch (error) { }


        var baoxiang = text("开宝箱得金币").findOne(3000);
        if (baoxiang) {
            toastLog("开始点击开宝箱");
            sleep(1000);
            click(baoxiang.bounds().centerX(), baoxiang.bounds().centerY() - 20);
            sleep(2000);
            check_guankanshipin();
            //进入视频播放页面
            // let close=id("v1").findOne(3000);
            // if(close) {
            //     log("点击关闭!回到视频播放界面");
            //     close.click();  
            // }
            sleep(3000)
            back()
            sleep(2000);
        }
        else {
            toastLog("没有发现开宝箱得金币");
        }
    }
    else {
        log("没有发现开宝箱！");
    }
}

app_func.main = function () {
    var index = 0;
    var count = 600
    var random_min = 2000
    var random_max = 6000
    var package_name = "com.ss.android.ugc.aweme.lite"
    var main_active = "com.ss.android.ugc.aweme.main.MainActivity"
    var w = device.width
    var h = device.height

    if (count == 0) { return }
    toastLog("即将滑动" + count + "次！");

    var open_app = launchAppEnhance(app_func.module_name);
    if (open_app) {
        while (index <= count) {
            var update = text("以后再说").findOne(1000);
            if (update) {
                sleep(1000);
                update.click();
                sleep(1000);
            }
            //生活费
            var bik = id("bik").findOne(1000);
            if (bik) {
                bik.click();
            }
            try { text('好的').findOne(200).click() } catch (e) { }
            if (index % 5 == 0) {
                check_kaibaoxiang();
            }
            sml_move(w / 2, h * 0.8 + h * random() * 0.1, w / 2, h * 0.15, 200 + 600 * random());
            var random_time = random(random_min, random_max);
            toastLog("观看" + app_func.module_name + "第" + index + "/" + count + "个视频中...");
            toastLog("预计时长" + (random_time / 1000) + "秒~");
            sleep(random_time);
            index++;
        }

    }
    else {
        toastLog("抖音极速版打开失败!");
    }
}

try {
    app_func.main();
} catch (error) {
    toastLog(error);
}

var storage = storages.create("xiangxiangyuedu");
storage.put("script_running_status", "end");
